home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / aros / dos / filepart.c < prev    next >
C/C++ Source or Header  |  1996-09-12  |  4KB  |  162 lines

  1. /*
  2.     (C) 1995 AROS - The Amiga Replacement OS
  3.     $Id: filepart.c,v 1.2 1996/08/23 17:06:17 digulla Exp $
  4.     $Log: filepart.c,v $
  5.     Revision 1.2  1996/08/23 17:06:17  digulla
  6.     Added #include "dos_intern.h"
  7.  
  8.     Revision 1.1  1996/08/20 11:58:36  digulla
  9.     FilePart by Martin Steigerwald
  10.  
  11.     Revision 1.0    1996/08/01 14:14:00     steigerwald
  12.     Untested first version!!!
  13.     Revision 1.1    1996/08/07 00:24:00     steigerwald
  14.     Revision 1.2    1996/08/20 12:42:99     steigerwald
  15.     Finally tested! ;-) It works, but I am not quite happy about how
  16.     it gets the result ;-((
  17.     Only rudimentary tests.
  18.  
  19.     Desc: Returns a pointer to the first char of the filename in the given
  20.       file part.
  21.     Lang: english
  22. */
  23. /* #define NO_AROS 1 */
  24. #include <dos/dos.h>
  25. #ifndef NO_AROS
  26. #include <aros/libcall.h>
  27. #include <clib/aros_protos.h>
  28. #include "dos_intern.h"
  29. #else
  30. #define __AROS_LH1(t,fn,a1,bt,bn,o,lib)     t fn (a1)
  31. #define __AROS_LHA(t,n,r)                   t n
  32. #define __AROS_FUNC_INIT
  33. #define __AROS_BASE_EXT_DECL(bt,bn)
  34. #define __AROS_FUNC_EXIT
  35. #endif
  36.  
  37.  
  38. /*****************************************************************************
  39.  
  40.     NAME */
  41.     #include <clib/dos_protos.h>
  42.  
  43.     __AROS_LH1(STRPTR, FilePart,
  44.  
  45. /*  SYNOPSIS */
  46.     __AROS_LHA(STRPTR, path, D1),
  47.  
  48. /*  LOCATION */
  49.     struct DosLibrary *, DOSBase, 145, Dos)
  50.  
  51. /*  FUNCTION
  52.     Get a pointer to the last component of a path, which is normally the
  53.     filename.
  54.  
  55.     INPUTS
  56.     path - pointer AmigaDOS path string
  57.         May be relative to the current directory or the current disk.
  58.  
  59.     RESULT
  60.     A pointer to the first char of the filename!
  61.  
  62.     NOTES
  63.  
  64.     EXAMPLE
  65.     FilePart("xxx:yyy/zzz/qqq") returns a pointer to the first 'q'.
  66.     FilePart("xxx:yyy")         returns a pointer to the first 'y'.
  67.     FilePart("yyy")             returns a pointer to the first 'y'.
  68.  
  69.     BUGS
  70.     None known.
  71.  
  72.     SEE ALSO
  73.     PathPart(), AddPart()
  74.  
  75.     INTERNALS
  76.     Goes from the last char of the pathname back until it finds a ':',
  77.     a '/' or until the first char reached.
  78.  
  79.     HISTORY
  80.     29-10-95    digulla automatically created from
  81.                 dos_lib.fd and clib/dos_protos.h
  82.  
  83.     04-08-96    steigerwald hopefully filled up with something useful
  84.                 ;-), however untested!
  85.  
  86.     07-08-96    steigerwald reworked code, implented digulla's
  87.                 suggestions, thanks Aaron ;-)
  88.  
  89.                 added some documentation ;-)
  90.  
  91.                 converted all comments in function to
  92.                 c++ style to avoid nested comments
  93.  
  94.                 again untested, cause too much AROS stuff
  95.                 that is not easy to #ifdef out missing
  96.  
  97.     20-08-96    steigerwald finally added all those #ifndef NO_AROS
  98.                 to get this thing working stand-alone
  99.                 test routine added
  100.                 some bugs fixed
  101.  
  102.                 problem: see while and ifs below ;-(((
  103.  
  104.                 routine seems to work so far, but doesnt
  105.                 check for path consistency so
  106.                 FilePart("dh0:test/exec:now") will give a
  107.                 pointer to "now" ;-)
  108.  
  109. *****************************************************************************/
  110. {
  111.     __AROS_FUNC_INIT
  112.     __AROS_BASE_EXT_DECL(struct DosLibrary *,DOSBase)
  113.  
  114.     if(path)
  115.     {
  116.     STRPTR i;
  117.  
  118.     /* set i to last char of path */
  119.  
  120.     if (!*path) /* path == "" ? */
  121.       return path;
  122.  
  123.     i = path + strlen (path) -1;   /* set i to the \0-byte */
  124.  
  125.     /* decrease pointer as long as there is no ':', no '/' or till
  126.       the first char anyway. hope this works in all situations */
  127.     while ((*i != ':') && (*i != '/') && (i != path))
  128.         i--;
  129.  
  130.     if ((*i == ':')) i++;
  131.     if ((*i == '/')) i++;
  132.  
  133.     return(i);
  134.     } /* path */
  135.  
  136.     return (0L);  /* if no path is given return NIL pointer */
  137.  
  138.     __AROS_FUNC_EXIT
  139. } /* FilePart */
  140.  
  141. #ifdef TEST
  142.  
  143. #include <stdio.h>
  144.  
  145. int main (int argc, char ** argv)
  146. {
  147.     // kommt noch ;-)
  148.  
  149.     UWORD i;
  150.     STRPTR s,fileptr;
  151.  
  152.     while (--argc)
  153.     {
  154.     s = *++argv;
  155.     fileptr = FilePart(s);
  156.  
  157.     printf("Pfad:  %s\nDatei: %s\n", s, fileptr);
  158.     }
  159. }
  160.  
  161. #endif /* TEST */
  162.